<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>立即执行函数</title>
</head>

<body>
    <script type="text/javascript">
        /*			function a(){
                				function b(){
                					var bbb = 234;
                					console.log(aaa);//123
                				}
                				var aaa = 123;
                				return b;
                			}
                			var glob = 100;
                			var demo = a();
                			demo();*/
        /*
         * 1,2,3. GO{
         * 		glob:undefined
         * 		demo:undefined
         * }
         * 
         * 4.GO{
         * 		glob:undefined
         * 		demo:undefined
         * 		a: function(){}
         * }
         * 5.GO{
         * 		glob:100
         * 		demo: undefined
         * }
         * 
         * 6.AO{
         * 		
         * }
         */

        /*function a(){
        	var num = 100;
        	function b(){
        		num++;
        		console.log(num);
        	}
        	return b;
        }
        var demo = a();
        demo();//101
        demo();//102*/

        /*function a(){
        	var num = 100;
        	function b(){
        		num++;
        		console.log(num);
        	}
        	return b;
        }
        var demo = a()();
        demo();// demo is not a function
        demo();*/

        /*
         * 闭包导致作用域链不释放，造成内存泄漏
         */
        /*function a(){
        	var aa = 345;
        	function b(){
        		function c(){
        			var cc = 123;
        		}
        		c();
        	}
        	b();
        }
        a();*/


        /*
         * 闭包的作用:
         *  1.实现共有变量
         * 		eg:函数累加器
         *  2.可以做缓存(存储结构)
         * 		eg:eater
         *  3.可以实现封装，实行私有化
         * 		eg:Person();
         *  4. 模块化开发，放置污染全局变量
         */

        /*
         * 累加器
         */

        /*function add(){
        	var count = 0;
        	function demo(){
        		count++;
        		console.log(count);
        	}
        	
        	return demo;
        }
			
        var counter = add();
        counter();*/

        /*
         * 可以做缓存(存储结构)
         */
        /*function test(){
        	var num = 100;
        	function a(){
        		num++;
        		console.log(num);
        	}
        	function b(){
        		num--;
        		console.log(num);
        	}
        	
        	return [a,b];
        }
        var myArr = test();
        myArr[0]();//101
        myArr[1]();//100*/

        /*function eater(){
        	var food = "";
        	var obj = {
        		eat : function(){
        			console.log("I am eating" + food);
        			food = "";
        		},
        		
        		push : function(myFood){
        			food = myFood;
        		}
        	}
        	return obj;
        }
			
        var eater1 = eater();
        eater1.push("Banana");
        eater1.eat();*/

        //==================================================立即执行函数========================

        //针对初始化功能的函数
        // (function(){}())//W3C建议第一种
        //(function(){})()
        //只有表达式才能被执行符号执行
        /*(function abc(a,b,c){
        	console.log(a + b + c);
        }(1,2,3));*/

        /*var num = (function (a,b,c){
        	var d = a + b + c * 2 -2;
        	return d;
        }(1,2,3));*/

        /*			//函数声明
        			function test(){
        				var a = 123;
        			}();//Uncaught SyntaxError: Unexpected token//叫函数声明
        			*/

        /*+ function test(){
        	console.log('a');
        }();//*/

        /*function test(a,b,c,d){
        	console.log(a + b + c + d);
        }(1,2,3,4);//不报错，不执行*/
    </script>
</body>

</html>